﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using System.Collections;
using System.Reflection;
using System.Collections.ObjectModel;

namespace Portal.Web.Utility
{
    public class WebPartUtility
    {
        private static WebPartTemplate[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            if (a == null) throw new Exception("Assembly " + filename + " not found or not valid");
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IWebPart))
                        {
                            WebPartTemplate w = (WebPartTemplate)a.CreateInstance(ty.FullName);
                            ArrayListModules.Add(w);
                        }
                    }
                }
            }
            WebPartTemplate[] ws = new WebPartTemplate[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (WebPartTemplate)ArrayListModules[iIndex];
            }
            return ws;
        }


        public static WebPartTemplate[] LoadPlugIn(string SiteId)
        {
            SiteParam oSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string Path = oSiteParam.DllPath;
            string[] Files = System.IO.Directory.GetFiles(Path, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    WebPartTemplate[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        if (asms[jIndex].Id != null)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Source = "";
                }
            }
            WebPartTemplate[] WebParts = new WebPartTemplate[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WebParts[iIndex] = (WebPartTemplate)ArrayListServives[iIndex];
            }

            return WebParts;
        }


        public static WebPartTemplate Find(string SiteId, string ServiceId)
        {
            WebPartTemplate[] found = LoadPlugIn(SiteId);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].Id != null)
                {
                    if (found[iIndex].Id.Trim().ToLower().Equals(ServiceId.Trim().ToLower()))
                    {
                        return found[iIndex];
                    }
                }
            }
            return null;
        }

        public static Collection<WebPartTemplate> LoadWebPartTemplateType(string SiteId)
        {
            SiteParam oSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            WebPartTemplate[] WebPartTemplates = LoadPlugIn(SiteId);
            Collection<WebPartTemplate> ColWebPartTemplates = new Collection<WebPartTemplate> { };
            Hashtable hs = new Hashtable();
            for (int iIndex = 0; iIndex < WebPartTemplates.Length; iIndex++)
            {
                WebPartTemplate OWebPartTemplate = WebPartTemplates[iIndex];
                try
                {
                    hs.Add(OWebPartTemplate, null);
                    ColWebPartTemplates.Add(WebPartTemplates[iIndex]);
                }
                catch { }
            }
            return ColWebPartTemplates;
        }
    }
}
